package com.jy.novel.controller.system;

import com.jy.novel.common.CommonResult;
import com.jy.novel.controller.base.BaseController;
import com.jy.novel.pojo.system.RoleInfo;
import com.jy.novel.service.cfg.WebMangerCfg;
import com.jy.novel.service.system.RoleService;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by jihb on 5/3/16.
 */
@Controller
@RequestMapping(value = "/rolepermission")
public class RolePermissionController extends BaseController {

    @Autowired
    RoleService roleService;

    @Autowired
    WebMangerCfg cfg;

    @RequestMapping(value = "/manager",method = RequestMethod.GET)
    public String manager(){
        return "system/rolepermission_manager";
    }

    @RequestMapping(value = "/rolelist",method = RequestMethod.GET)
    @ResponseBody
    public CommonResult roleList(
            @RequestParam(value = "start", required = false) String start,
            @RequestParam(value = "limit", required = false) String limit,
            @RequestParam(value = "curPage",required = false,defaultValue = "1")String curPage,
            @RequestParam(value = "pageSize",required = false,defaultValue = "20")String pageSize
    ){
        if (start != null) {
            curPage = String.valueOf(NumberUtils.toInt(start, 0) / NumberUtils.toInt(limit, 10) + 1);
            pageSize = limit;
        }
        List<RoleInfo> roleInfos = roleService.findRoleList(NumberUtils.toInt(curPage),NumberUtils.toInt(pageSize));
        return CommonResult.returnDataWrap(roleInfos);
    }

    @RequestMapping(value = "/roledetail",method = RequestMethod.GET)
    @ResponseBody
    public Object roleDetail(
            @RequestParam(value = "id")String id
    ){
       return roleService.getRoleInfo(NumberUtils.toLong(id));
    }

    @RequestMapping(value = "/addrole",method = RequestMethod.POST)
    @ResponseBody
    public CommonResult addRole(
            @ModelAttribute RoleInfo roleInfo
    ){

        boolean bl = roleService.addRole(roleInfo);
        return returnBoolWrap(bl,"添加角色失败");
    }

    @RequestMapping(value = "/updaterole",method = RequestMethod.POST)
    @ResponseBody
    public CommonResult updateRole(
            @RequestParam(value = "id")String id,
            @RequestParam(value = "name")String name,
            @RequestParam(value = "remark")String remark
    ){

        boolean bl = roleService.updateRoleInfo(NumberUtils.toLong(id),name,remark);
        return returnBoolWrap(bl,"添加角色失败");
    }

    @RequestMapping(value = "/deleterole",method = RequestMethod.POST)
    @ResponseBody
    public CommonResult deleteRole(
            @RequestParam(value = "id")String id
    ){
        if (NumberUtils.toLong(id)==cfg.getRoleAdminId()){
            return CommonResult.returnFailure("系统管理员拥有最高权限无法删除");
        }
        if (NumberUtils.toLong(id)==cfg.getRoleDefaultId()){
            return CommonResult.returnFailure("系统默认角色不能删除");
        }
        boolean bl  = roleService.deleteRole(NumberUtils.toLong(id),cfg.getRoleDefaultId());
        return returnBoolWrap(bl,"删除角色失败");
    }
}
